<template>
  <div>

    <span>ele-table单选</span>
    <el-table
        ref="singleTable"
        :data="tableData"
        border
        height=220
        highlight-current-row
        stripe
        style="width: 100%"
        @select="dialogCheck"
        @current-change="handleCurrentChange">
      <el-table-column align="center" type="selection" width="50"></el-table-column>
      <el-table-column type="index" width="50"/>
      <el-table-column label="日期" property="date" width="120"/>
      <el-table-column label="姓名" property="name" width="120"/>
      <el-table-column label="年龄" property="age">
        <template slot-scope="scope">
          <el-input-number v-model=" scope.row.age" :min="1" :step="1" size="mini"
                           step-strictly>
          </el-input-number>
        </template>
      </el-table-column>
      <el-table-column label="年龄*2" property="age">
        <template slot-scope="scope">
          <span>{{scope.row.age * scope.row.num}}</span>
        </template>
      </el-table-column>
      <el-table-column label="地址" property="address"/>
      <el-table-column align="center" header-align="center" label="删除">
        <template slot-scope="scope">
          <el-button type="primary" @click="tableData.splice(scope.$index, 1)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 20px">
      <el-button @click="setCurrent(tableData[1])">选中第二行</el-button>
      <el-button @click="setCurrent()">取消选择</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectioned: '',//单选中的值
      tableData: [{
        date: '2016-05-02',
        name: '王小虎',
        age: 18,
        num: 2,
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-04',
        name: '王小虎', age: 5, num: 4,
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        date: '2016-05-01',
        name: '王小虎', age: 18, num: 1,
        address: '上海市普陀区金沙江路 1519 弄'
      }, {
        date: '2016-05-03',
        name: '王小虎', age: 18, num: 3,
        address: '上海市普陀区金沙江路 1516 弄'
      }],
      currentRow: null
    }
  },

  methods: {
    dialogCheck(selection, row) {
      this.$refs.singleTable.clearSelection()
      this.selectioned = {} //这里清除选择的记忆；主要是避免取消掉原来选中的还残留原来选中的东西
      if (selection.length === 0) { // 判断selection是否有值存在
        return
      }
      if (row) {
        this.selectioned = row
        this.$refs.singleTable.toggleRowSelection(row, true)
      }
    },
    setCurrent(row) {
      this.$refs.singleTable.setCurrentRow(row);
    },
    handleCurrentChange(val) {
      this.currentRow = val;
      console.log(val)
      this.$refs.singleTable.setCurrentRow(val);
    }
  }
}
</script>
<style lang="less" scoped>
// cnpm  install less@3.9.0 less-loader@4.1.0 --save-dev
/deep/ .has-gutter {
  .el-checkbox {
    display: none;
  }
}
</style>
